/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wallythewatermelon;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author mgohde, mweidman
 */
public class WallyTheWatermelon extends JPanel implements Runnable {
    
    public static final int WINDOW_WIDTH = 1024;//1024;
    public static final int WINDOW_HEIGHT = 512;//512;
    public static final int FRAME_DELAY = 50;
    private static boolean animate = true;
    public boolean isWalkingLeft=false;
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final WallyTheWatermelon content = new WallyTheWatermelon();
        
        JFrame frame = new JFrame();
        
        Color backgroundColor = Color.CYAN;
        frame.setBackground(backgroundColor);
        content.setBackground(backgroundColor);
        
        frame.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setContentPane(content);
        frame.pack();
        
        frame.addKeyListener(new KeyDetector());
        frame.addMouseListener(new MouseDetector());
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {System.exit(0);}
            @Override
            public void windowDeiconified(WindowEvent e) {content.start();}
            @Override
            public void windowIconified(WindowEvent e) {content.stop();}
        });
        
        DrawManager.initLevels();
        content.run();
    }
    
    /** Runs animation at the speed of FRAME_DELAY */
    @Override
    public void run() {
        while (true) {
            if (animationEnabled()) {
                super.repaint();
            }
            try {
                Thread.sleep(FRAME_DELAY);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    
    /** Allows animation to start */
    public synchronized void start() {
        animate = true;
    }
    
    /** Pauses animation */
    public synchronized void stop() {
        animate = false;
    }
    
    /** Returns whether animation should continue (synchronized) */
    public synchronized boolean animationEnabled() {
        return animate;
    }
    
    /** Called every FRAME_DELAY ms */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        DrawManager.go(g2);
    }
    
    /** Returns an image from textures folder with name str */
    public static Image getImage(String str) {
        Image img = null;
        try {
            img = ImageIO.read(new File("textures/" + str + ".png"));
        } catch (IOException ex) {
            Logger.getLogger(WallyTheWatermelon.class.getName()).log(Level.SEVERE, null, ex);
        }
        return img;
    }
}
